package client.ui.theme.defaultTheme;

import java.awt.event.WindowEvent;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

import client.engine.ChatEngine;
import client.ui.IChatRoomTab;
import client.ui.IChatRoomWindow;

public class DefaultChatRoomWindow extends javax.swing.JFrame implements IChatRoomWindow {

	// Variables declaration - do not modify
    /*private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;*/
    private javax.swing.JTabbedPane chatTabsPane;
    
    private Icon closeTabIcon;
	private transient Rectangle mPosition = null;

    // End of variables declaration
    
    private ChatEngine engine = null;
    private ArrayList<IChatRoomTab> tabs = new ArrayList<IChatRoomTab>();
	
    /** Creates new form NewJFrame1 */
    public DefaultChatRoomWindow() {
    	closeTabIcon = new ImageIcon("./clientImages/closeTab.gif");
        initComponents();
        setSize(600, 600);
       // paintCloseTabIcon(g, 0, 0);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        chatTabsPane = new javax.swing.JTabbedPane();
        /*jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();*/

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        this.setTitle("Parlanchin - Conversation");
        
        /*jMenu1.setText("File");
        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);*/

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(chatTabsPane, javax.swing.GroupLayout.DEFAULT_SIZE, 460, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(chatTabsPane, javax.swing.GroupLayout.DEFAULT_SIZE, 316, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    private void paintCloseTabIcon(Graphics g, int x, int y)
    {
    	chatTabsPane.addMouseListener( new MouseAdapter()
			{
				@Override public void mouseReleased( MouseEvent e )
				{
					// asking for isConsumed is *very* important, otherwise more than one tab might get closed!
					if ( !e.isConsumed()  &&   mPosition.contains( e.getX(), e.getY() ) )
					{
						final int index = chatTabsPane.getSelectedIndex();
						chatTabsPane.remove( index );
						e.consume();
					}
				}
			});
    	
    	mPosition = new Rectangle( x,y, getIconWidth(), getIconHeight() );
   		closeTabIcon.paintIcon(this, g, x, y );
    }
    
    private int getIconWidth()
    {
    	return closeTabIcon.getIconWidth();
    }
    
    private int getIconHeight()
    {
    	return closeTabIcon.getIconHeight();
    }
    
    @Override
	public void addTab(IChatRoomTab tab) {

		tabs.add(tab);
		chatTabsPane.removeAll();
		
		for(int i = 0; i < tabs.size(); i++){
			chatTabsPane.add("Conversation " + (i+1), (JPanel) tabs.get(i));
		}
		
	}

	@Override
	public void removeTab(IChatRoomTab tab) {

		tabs.remove(tab);
		
		chatTabsPane.removeAll();
		
		for(int i = 0; i < tabs.size(); i++){
			chatTabsPane.add("Conversation " + (i+1), (JPanel) tabs.get(i));
		}
		
	}

	@Override
	public void setChatEngine(ChatEngine engine) {

		this.engine = engine;
		
	}

	@Override
	public void showWindow() {
		
		this.setVisible(true);
		
	}    

	private void formWindowClosing(WindowEvent evt) {
		
		closeWindow();
		
	}

	@Override
	public void closeWindow() {
		
		while(tabs.size() != 0){
			IChatRoomTab tab = tabs.get(0);
			tab.closeTab();
			removeTab(tab);
		}
		
		engine.closeWindow(this);
		this.dispose();
	}
}
